##########################################################################################################################################################
### Setup

# Clear workspace
rm(list=ls())

# Load packages
library(stringr)
library(sjPlot)
library(tidyverse)
library(magrittr)
library(abmisc)
library(patchwork)

# Load data
load("data/nc_votes.RData")


### Format data

# Make factors
partyVotesWSpon3$session.x <- as.factor(partyVotesWSpon3$session.x)
voteLawVars3$session <- as.factor(voteLawVars3$session)
partyVotesWSpon3$chamber <- as.factor(partyVotesWSpon3$chamber)

# Make a binary veto indicator that is based on session (had it after the 1995 session)
partyVotesWSpon3$veto <- ifelse((partyVotesWSpon3$session.x==1991|partyVotesWSpon3$session.x==1993|
                                   partyVotesWSpon3$session.x==1995), 0, 1)

voteLawVars3$veto <- ifelse((voteLawVars3$session==1991|voteLawVars3$session==1993|
                               voteLawVars3$session==1995), 0, 1)

# Limit to final vote
partyVotesWSpon3$description <- tolower(str_squish(partyVotesWSpon3$description))
lastVote <- partyVotesWSpon3[str_detect(partyVotesWSpon3$description, "(passed( 2nd \\&)* 3rd reading)|(^adopted$)|(^rec from (senate|house)|r3|third reading|passes its third|passes its second)"),]

# Create band variables
voteLawVars3$band1session <- ifelse((voteLawVars3$session==1995|voteLawVars3$session==1997),1,0)
lastVote$band1session <- ifelse((lastVote$session.x==1995|lastVote$session.x==1997),1,0)

# Final dataframes
band1session_voteLaw <- voteLawVars3[voteLawVars3$band1session==1,]
band1session_lastVote <- lastVote[lastVote$band1session==1,]

# Recode binary variables
band1session_voteLaw %<>% mutate(veto = ifelse(veto==1, "Yes", "No") %>% as.factor() %>% relevel(ref='No'))
band1session_lastVote %<>% mutate(veto = ifelse(veto==1, "Yes", "No") %>% as.factor() %>% relevel(ref='No'))



#########################################################################################################################################################
### Modeling

### Run models

# Pr(vote)
mod_vote <- glm(gotVote ~ veto + maj_sponsor  + maj_share + chamber, family='binomial', data=band1session_voteLaw)

# Pr(law)
mod_law <- glm(becameLaw ~ veto + maj_sponsor  + maj_share + chamber, family='binomial', data=band1session_voteLaw)

# Prop(yea)
mod_yea <- lm(passagePro ~ veto + maj_sponsor + maj_share + chamber,  data=band1session_lastVote)

# Prop(yea|min)
mod_min_yea <- lm(min_yea_prop ~ veto + maj_sponsor + maj_share + chamber,  data=band1session_lastVote)

# Pr(unity)
mod_unity <- glm(unity_vote_5 ~ veto + maj_sponsor + maj_share  + chamber, family='binomial', data=band1session_lastVote)


### Substantive effect sizes

# DV standard deviations
vote_sd <- sd(band1session_voteLaw$gotVote, na.rm=TRUE)
law_sd <- sd(band1session_voteLaw$becameLaw, na.rm=TRUE)
yea_sd <- sd(band1session_lastVote$passagePro, na.rm=TRUE)
yea_min_sd <- sd(band1session_lastVote$min_yea_prop, na.rm=TRUE)
unity_sd <- sd(band1session_lastVote$unity_vote_5, na.rm=TRUE)

# Effect sizes
get_eff_size <- function(model, terms, type){
  eff_size <- get_model_data(model, terms=terms, type=type) %>% 
    as.data.frame() %>% 
    pull(predicted) %>%
    diff()
  eff_size
}
eff_vote <- abs(get_eff_size(mod_vote, 'veto', 'pred')/vote_sd)
eff_law <- abs(get_eff_size(mod_law, 'veto', 'pred')/law_sd)
eff_yea <- abs(get_eff_size(mod_yea, 'veto', 'pred')/yea_sd)
eff_min_yea <- abs(get_eff_size(mod_min_yea, 'veto', 'pred')/yea_min_sd)
eff_unity <- abs(get_eff_size(mod_unity, 'veto', 'pred')/unity_sd)


### Substantive effect plots

# Pr(Vote)
plot_vote <- plot_model(mod_vote, type='pred', line.size=2, dot.size=5,
                                    terms=c('veto'), colors='bw') +
  labs(x='Governor Holds Veto Power', y='Probability of Reaching a Vote', title='') + 
  theme_bw() +
  annotate(geom='text', x=1.85, y=0.353, label=paste0(round(eff_vote, 2), " SDs"), size=6) +
  theme(text=element_text(size=18),
        axis.text=element_text(size=18))

# Pr(law)
plot_law <- plot_model(mod_law, type='pred', line.size=2, dot.size=5,
                        terms=c('veto'), colors='bw') +
  labs(x='Governor Holds Veto Power', y='Probability of Becoming Law', title='') + 
  theme_bw() +
  annotate(geom='text', x=1.85, y=0.252, label=paste0(round(eff_law, 2), " SDs"), size=6) +
  theme(text=element_text(size=18),
        axis.text=element_text(size=18))

# Prop(yea)
plot_yea <- plot_model(mod_yea, type='pred', line.size=2, dot.size=5,
                        terms=c('veto'), colors='bw') +
  labs(x='Governor Holds Veto Power', y='Proportion of Members Voting Yea', title='') + 
  theme_bw() +
  annotate(geom='text', x=1.85, y=0.951, label=paste0(round(eff_yea, 2), " SDs"), size=6) +
  theme(text=element_text(size=18),
        axis.text=element_text(size=18))

# Prop(yea|min)
plot_yea_min <- plot_model(mod_min_yea, type='pred', line.size=2, dot.size=5,
                        terms=c('veto'), colors='bw') +
  labs(x='Governor Holds Veto Power', y='Proportion of Minority Members Voting Yea', title='') + 
  theme_bw() +
  annotate(geom='text', x=1.85, y=0.9349, label=paste0(round(eff_min_yea, 2), " SDs"), size=6) +
  theme(text=element_text(size=18),
        axis.text=element_text(size=18))

# Pr(unity)
plot_unity <- plot_model(mod_unity, type='pred', line.size=2, dot.size=5,
                           terms=c('veto'), colors='bw') +
  labs(x='Governor Holds Veto Power', y='Probability of Party Unity Vote', title='') + 
  theme_bw() +
  annotate(geom='text', x=1.85, y=0.057, label=paste0(round(eff_min_yea, 2), " SDs"), size=6) +
  theme(text=element_text(size=18),
        axis.text=element_text(size=18))


### Combine and save

# Combine
nc_plots <- plot_vote + plot_law + plot_yea + plot_yea_min + plot_unity + plot_layout(ncol=2)

# Save
ggsave("results/fig5_nc_plot.pdf", width=12, height=15, plot=nc_plots)
